/*
*  Copyright (c) 2001 Sun Microsystems, Inc.  All rights
*  reserved.
*
*  Redistribution and use in source and binary forms, with or withouta
*  modification, are permitted provided that the following conditions
*  are met:
*
*  1. Redistributions of source code must retain the above copyright
*  notice, this list of conditions and the following disclaimer.
*
*  2. Redistributions in binary form must reproduce the above copyright
*  notice, this list of conditions and the following discalimer in
*  the documentation and/or other materials provided with the
*  distribution.
*
*  3. The end-user documentation included with the redistribution,
*  if any, must include the following acknowledgment:
*  "This product includes software developed by the
*  Sun Microsystems, Inc. for Project JXTA."
*  Alternately, this acknowledgment may appear in the software itself,
*  if and wherever such third-party acknowledgments normally appear.
*
*  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
*  must not be used to endorse or promote products derived from this
*  software without prior written permission. For written
*  permission, please contact Project JXTA at http://www.jxta.org.
*
*  5. Products derived from this software may not be called "JXTA",
*  nor may "JXTA" appear in their name, without prior written
*  permission of Sun.
*
*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
*  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
*  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
*  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
*  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
*  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*  SUCH DAMAGE.
*  ====================================================================
*
*  This software consists of voluntary contributions made by many
*  individuals on behalf of Project JXTA.  For more
*  information on Project JXTA, please see
*  <http://www.jxta.org/>.
*
*  This license is based on the BSD license adopted by the Apache Foundation.
*
*  $Id: Constants.java,v 1.14 2006/07/13 05:26:40 nano Exp $
*/

package net.jxta.myjxta.util;

import net.jxta.ext.config.Resource;
import net.jxta.ext.config.ResourceNotFoundException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;

/**
 *
 * @version $Id: Constants.java,v 1.14 2006/07/13 05:26:40 nano Exp $
 *
 * @author james todd [gonzo at jxta dot org]
 */

public class Constants
extends Resource {
    
    public static final String NAME = "/myjxta/application/@name";
    public static final String VERSION = "/myjxta/application/@version";
    public static final String BUILD = "/myjxta/application/@build";
    public static final String PROXY = "/myjxta/proxy";
    public static final String PROXY_HTTP = PROXY + "/@http";
    public static final String NETWORK = "/myjxta/network";
    public static final String NETWORK_ID = NETWORK + "/@id";
    public static final String NETWORK_NAME = NETWORK + "/@name";
    public static final String NETWORK_DESCRIPTION = NETWORK + "/@description";
    public static final String GROUP = NETWORK + "/group";
    public static final String GROUP_ID = GROUP + "/@id";
    public static final String GROUP_NAME = GROUP + "/@name";
    public static final String GROUP_PASSWORD = GROUP + "/@password";
    public static final String GROUP_DESCRIPTION = GROUP + "/@description";
    public static final String STRING_RESOURCES = "/myjxta/resources/@strings";
    public static final String THEME_RESOURCES = "/myjxta/resources/@theme";
    public static final String STYLE_RESOURCES = "/myjxta/resources/@chatStyle";
    public static final String LOG4J_BUNDLE = "net/jxta/myjxta/resources/text/log4jText";
    public static final String ABOUT_HTML = "/net/jxta/myjxta/resources/html/about.html";
    public static final String ROOT_NODE = "JXTA-Root";
    public static final String JXTA_RESOURCE = "jxta";
    
    public static final String RENDEZVOUS_BOOTSTRAP =
        "/jxta/network/rendezVous/@bootstrap";
    public static final String RELAYS_BOOTSTRAP = "/jxta/network/relays/@bootstrap";
    
    public static final String DEFAULT_RENDEZVOUS_BOOTSTRAP =
        "http://rdv.jxtahosts.net/cgi-bin/rendezvous.cgi?2";
    public static final String DEFAULT_RELAYS_BOOTSTRAP =
        "http://rdv.jxtahosts.net/cgi-bin/relays.cgi?2";
    
    public static final String NET_PEER_GROUP_ID_KEY = "NetPeerGroupID";
    public static final String NET_PEER_GROUP_NAME_KEY = "NetPeerGroupName";
    public static final String NET_PEER_GROUP_DESCRIPTION_KEY =
        "NetPeerGroupDesc";
    
    public static final String PROTOCOL_FILE = "file";
    public static final String PROTOCOL_HTTP = "http";
    
    public static final int DEFAULT_HTTP_PROXY_PORT = 80;

    /**
     * CRLF constant ensures we don't have to endure the hard-coded "\n" herasy.
     */
    public static final String CRLF = System.getProperty("line.separator");

    public static final int AUTO_RENDEZVOUS_JOIN_PERIOD = 2 * 60 * 1000;
    public static final int AUTO_RENDEZVOUS_CREATE_PERIOD = 3 * 1000;

    private static final int BLOCK = 4 * 1024;
    private static Constants instance = null;
    
    static {
        Env.initialize();
    }
    
    public static Constants getInstance() {
        if (instance == null) {
            instance = new Constants();
        }
        
        return instance;
    }

    public void save() {
        save(getConstantsFile());
    }

    public void save(File f) {
        if (f != null) {
            if (f.isDirectory()) {
                f = new File(f, new File(Env.CONSTANTS).getName());
            }
            
            File p = f.getParentFile();
            
            if (! p.exists()) {
                p.mkdirs();
            }
            
            FileWriter w = null;
            
            try {
                w = new FileWriter(f, false);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            
            if (w != null) {
                try {
                    f.createNewFile();
                    
                    StringReader r = new StringReader(toString());
                    char[] buf = new char[BLOCK];
                    int l = 0;
                    
                    while ((l = r.read(buf, 0, BLOCK)) > -1) {
                        w.write(buf, 0, l);
                    }
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                } finally {
                    if (w != null) {
                        try {
                            w.close();
                        } catch (IOException ioe) {
                            ioe.printStackTrace();
                        }
                    }
                }
            }
        }
    }
    
    protected Constants() {
        boolean loaded = false;

        try {
            load(getConstantsFile());
            
            loaded = true;
        } catch (MalformedURLException mue) {
        } catch (ResourceNotFoundException rnfe) {
        }
        
        if (! loaded) {
            try {
                load(getClass().getResourceAsStream(Env.CONSTANTS));
            } catch (ResourceNotFoundException rnfe) {
            }
        }
    }

    private File getConstantsFile() {
        File f = null;

        try {
            f = new File(Conversion.toFile(Env.getHome()),
                new File(Env.CONSTANTS).getName());
        } catch (ConversionException e) {
            e.printStackTrace();
        }

        return f;
    }
}
